﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LongSCADA_WPF.Models
{
    public partial class LogModel:EntityBase
    {
        private DateTime _timestamp;
        public DateTime Timestamp
        {
            get => _timestamp;
            //时间戳
            set => SetProperty(ref _timestamp, value);
        }
        private string _level;
        public string Level
        {
            get => _level;
            //日志等级
            set => SetProperty(ref _level, value);
        }
        private string _logger;
        public string Logger
        {
            get => _logger;
            //日志记录器
            set => SetProperty(ref _logger, value);
        }
        private string _message;
        public string Message
        {
            get => _message;
            //具体通知内容
            set => SetProperty(ref _message, value);
        }

        /// <summary>
        /// 解析日志行
        /// 2025-08-29 14:14:00.1706|ERROR|LongSCADA_WPF.ViewModels.MainViewModel|PLC 连接失败:127.0.0.1:102 
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public static LogModel? Parse(string line)
        {
            try
            {
                string[] parts = line.Split('|');
                if (parts.Length >= 4)
                {
                    DateTime timestamp = DateTime.Parse(parts[0].Trim());
                    string level = parts[1].Trim();
                    string logger = parts[2].Trim();
                    string message = parts[3].Trim();
                    return new LogModel()
                    {
                        Timestamp = timestamp,
                        Level = level,
                        Logger = logger,
                        Message = message
                    };
                }
            }
            catch ( Exception ex)
            {
                Debug.WriteLine(ex.Message);
               
            }
            return null;
        }
    }
}
